package yawning.mew.character;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

import yawning.mew.equipment.EquipSet;
import yawning.mew.equipment.EquipSet.Slot;
import yawning.mew.equipment.RawItem;
import yawning.mew.equipment.RawItem.Property;

public class EquipSetToonBuilder {
	private Toon mBaseToon;
	private EquipSet mEquipSet;

	public EquipSetToonBuilder setBaseToon(Toon baseToon) {
		mBaseToon = baseToon;
		return this;
	}

	public EquipSetToonBuilder setEquipSet(EquipSet es) {
		mEquipSet = es;
		return this;
	}

	public Toon build() {
		Toon t = new Toon();

	    t.level = mBaseToon.level;
	    t.mClass = mBaseToon.mClass;
	    t.mRace = mBaseToon.mRace;

	    t.mGlyphs = new HashSet<Glyph>(mBaseToon.mGlyphs);
	    t.mTalents = Arrays.copyOf(mBaseToon.mTalents, mBaseToon.mTalents.length);
	    t.mTalentSpec = mBaseToon.mTalentSpec;
	    t.mProfessions = new HashMap<Profession,Integer>(mBaseToon.mProfessions);

	    StatLogic statLogic = StatLogic.get(t);

    	t.strength = mEquipSet.sumProperty(Property.STRENGTH) + statLogic.getBaseStrength(t.mRace);
    	t.agility = mEquipSet.sumProperty(Property.AGILITY) + statLogic.getBaseAgility(t.mRace);
    	t.stamina = mEquipSet.sumProperty(Property.STAMINA) + statLogic.getBaseStamina(t.mRace);
    	t.intellect = mEquipSet.sumProperty(Property.INTELLECT) + statLogic.getBaseIntellect(t.mRace);
    	t.spirit = mEquipSet.sumProperty(Property.SPIRIT) + statLogic.getBaseSpirit(t.mRace);
    	t.armor = mEquipSet.sumProperty(Property.ARMOR);
    	t.bonusArmor = mEquipSet.sumProperty(Property.BONUS_ARMOR);
    	t.attackPower = mEquipSet.sumProperty(Property.ATTACK_POWER);
    	t.mDodgeRating = mEquipSet.sumProperty(Property.DODGE_RATING);
    	t.mParryRating = mEquipSet.sumProperty(Property.PARRY_RATING);
    	t.hasteRating = mEquipSet.sumProperty(Property.HASTE_RATING);
    	t.critRating = mEquipSet.sumProperty(Property.CRIT_RATING);
    	t.hitRating = mEquipSet.sumProperty(Property.HIT_RATING);
    	t.expertiseRating = mEquipSet.sumProperty(Property.EXPERTISE_RATING);
    	t.masteryRating = mEquipSet.sumProperty(Property.MASTERY_RATING);

    	if (mEquipSet.mEquipmentMap.containsKey(Slot.MAIN_HAND)) {
	    	RawItem rawItem = mEquipSet.mEquipmentMap.get(Slot.MAIN_HAND).mItem;
	    	t.weaponTop = rawItem.properties.get(Property.MELEE_DMG_MAX).intValue();
	    	t.weaponBottom = rawItem.properties.get(Property.MELEE_DMG_MIN).intValue();
	    	t.weaponSpeed = (int) Math.round(rawItem.properties.get(Property.MELEE_SPEED).doubleValue() * 1000);
    	}

    	t.isArmorSpec = (t.level >= 50) && mEquipSet.checkArmorSpec(t.mClass.mArmorSubclass);

		return t;
	}
}
